#coding=utf-8

import re,urllib2
from debug import logprint
import datetime,time
import model
from model import get_code_need,update_daily_quote

today=datetime.date.today()
cyear=str(today)[:4]


def get_zdzf(quote):
    re_zdzf="<span class=\"zdf \w+ spanadd\">(\-?[0-9]+\.[0-9]+)%</span>"
    zdzf=re.findall(re_zdzf,quote)
    #logprint(zdzf)
    
    #输出格式为['127.37', '-14.78', '11.64', '66.27', '110.58', '147.07']
    #数据不全处理
    if len(zdzf)<6:
        for i in range(len(zdzf),6):
            logprint('error: for zdzf list '+str(i))
            zdzf.append('0.00')
    
    return zdzf

def get_tlpm(quote):
    re_tlpm="<li class=\"tlpm\">([0-9]+)<span class=\"gray\">\|</span>([0-9]+)</li>"
    tlpm=re.findall(re_tlpm,quote)
    #logprint(tlpm)
    
    if len(tlpm)<6:
        for i in range(len(tlpm),6):
            logprint('error: for tlpm list '+str(i))
            tlpm.append(('0','0'))
    
    #输出格式为[('6', '363'), ('571', '618'), ('12', '528'), ('5', '406'), ('9', '359'), ('27', '300')]
    return tlpm    

def get_cday_value(htmlstr):
    re_value="<span class='left12'><span class=\"\w+ bold\">([0-9]+\.[0-9]+)</span>"
    #re_value="</a>\(06\-19\)：</span><span class='left12'><span class=\"\w+ bold\">([0-9]+\.[0-9]+)</span>"
    cday_value=re.findall(re_value,htmlstr)
    #logprint(cday_value)
    
    if len(cday_value)>=1:
        cv=cday_value[0]
    else:
        cv="null"
        
    #logprint(cv)
    return cv

def get_vday(htmlstr):
    #print htmlstr
    re_vdaystr="<span class='left11'>(.+)</span><span class='left12"
    re_vday="([0-9]+\-[0-9]+)"
    
    vdaystr=re.findall(re_vdaystr,htmlstr)
    vday=re.findall(re_vday,vdaystr[0])
    
    return cyear+'-'+vday[0]
    
def get_fund_daily_quote(code):
    fdata={}
    
    furl='http://fund.eastmoney.com/%s.html'%code
    logprint(furl)
    htmlstr= urllib2.urlopen(furl,timeout=60).read()
    
    
    """
    f=open('dmfund2.log','r')
    lines=f.readlines()
    flist=[]
    for line in lines:
        flist.append(line)
    f.close()
    
    htmlstr=''.join(flist)
    """
    
    re_quote="id=\"jdzfTab0\"([\s\S]+)id=\"jdzfTab1\""
    quote=re.findall(re_quote,htmlstr)
    quote=quote[0].decode("gb2312").encode("utf-8")
    
    #存入代码
    fdata['code']=code
    #获取净值日期
    vday=get_vday(htmlstr)
    if vday:
        fdata['date']=vday
    
    #获取当日净值
    cv=get_cday_value(htmlstr)
    if cv:
        fdata['cvalue']=cv
    
    #获取阶段涨幅
    zdzf=get_zdzf(quote)
    if len(zdzf)>=1:
        fdata['zdzf_cyear']=float(zdzf[0])
        fdata['zdzf_week']=float(zdzf[1])
        fdata['zdzf_month']=zdzf[2]
        fdata['zdzf_3month']=zdzf[3]
        fdata['zdzf_6month']=zdzf[4]
        fdata['zdzf_year']=zdzf[5]
    else:
        logprint(fdata)
        logprint('Error:fdata length is '+str(len(fdata)))
        
    #获取分类排名    
    tlpm=get_tlpm(quote)
    if len(tlpm)>=1:
        fdata['tlpm_cyear']=tlpm[0][0]
        fdata['tlpm_week']=tlpm[1][0]
        fdata['tlpm_month']=tlpm[2][0]
        fdata['tlpm_3month']=tlpm[3][0]
        fdata['tlpm_6month']=tlpm[4][0]
        fdata['tlpm_year']=tlpm[5][0]
    
    
    
    logprint(fdata)
    return fdata

def sortedDictValues(adict):
    keys = adict.keys()
    keys.sort(reverse=False)
    #return [adict[key] for key in keys]
    return [(key,adict[key]) for key in keys]

def sortedDictValues2(adict): 
    keys = adict.keys() 
    keys.sort() 
    return map(adict.get,keys)

def sortedDictValues3(adict): 
    return sorted(adict.items(), key=lambda e:e[0], reverse=False)



def hashtest():
    import hashlib
    md5 = hashlib.md5() #创建一个MD5加密对象  
    md5.update("JGood is a handsome boy")  #更新要加密的数据  
    #print hashlib.new("md5", "JGood is a handsome boy").hexdigest()    
    #print url


def get_org_htmlstr():
    #baseURL='http://finance.yahoo.com/q/op?s=%s'%(symb)
    #baseURL='http://data.tsci.com.cn/US/US_List.aspx?q=A102'
    baseURL='http://en.wikipedia.org/wiki/NASDAQ-100'
    htmlstr= urllib2.urlopen(baseURL,timeout=60).read()
    
    #print htmlstr
    return htmlstr    
 
def filter_symb(htmlstr):
    re_cdata=re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #�归�CDATA
    re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
    re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
    re_p=re.compile('<P\s*?/?>')#澶���㈣�
    re_h=re.compile('</?\w+[^>]*>')#HTML���
    re_comment=re.compile('<!--[^>]*-->')#HTML娉ㄩ�
    s=re_cdata.sub('',htmlstr)#�绘�CDATA
    s=re_script.sub('',s) #�绘�SCRIPT
    s=re_style.sub('',s)#�绘�style
    s=re_p.sub('\r\n',s)#灏�p>杞��涓烘�琛�
    s=re_h.sub('',s) #�绘�HTML ���
    s=re_comment.sub('',s)#�绘�HTML娉ㄩ�  
    blank_line=re.compile('\n+')#�绘�澶����┖琛�
    s=blank_line.sub('\n',s)
    return s 

def filter_line(line):
    re_name="\">(.+)</a>"
    re_symb="</a>(.+)</li>"
    #re_symb2="\((.+)\)"
    #re_symb="\((.+)\)"
    name=re.findall(re_name,line)
    symb=re.findall(re_symb,line)
    s1=symb[-1].lstrip(' (')
    #print s1
    s2=s1.rstrip(')')
    
    return {'name':name[0],'symb':s2}

def get_fromtxt():
    #print name
    f=open('c100.txt','r')
    lines=f.readlines()
    
    symb_dict=[]
    for line in lines:
        #print line
        symb_dict.append(filter_line(line))
        
    f.close()
    
    return symb_dict

def daily_quote_run():
    codelist=get_code_need()
    logprint(codelist)
    
    dblist=[]
    for code in codelist:
        code_quote=get_fund_daily_quote(code[0])
        dblist.append(code_quote)
        
    update_daily_quote(dblist)

if __name__ == "__main__":
    #hashtest()
    #get_org_htmlstr()
    daily_quote_run()